package com.mla.fastdfs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mla.fastdfs.commom.RedisConstant;
import com.mla.fastdfs.dto.RoleDto;
import com.mla.fastdfs.dto.UserDto;
import com.mla.fastdfs.mapper.PermissionMapper;
import com.mla.fastdfs.mapper.RoleMapper;
import com.mla.fastdfs.mapper.UserMapper;
import com.mla.fastdfs.mapper.UserRoleMapper;
import com.mla.fastdfs.pojo.Permission;
import com.mla.fastdfs.pojo.User;
import com.mla.fastdfs.pojo.UserRole;
import com.mla.fastdfs.service.UserService;
import com.mla.fastdfs.utils.DateUtil;
import com.mla.fastdfs.utils.PasswordUtil;
import com.mla.fastdfs.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Jun
 * @version v1.0
 * @date 2020-08-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private PermissionMapper permissionMapper;


    public void insert(User record) {
        if (record.getPassword() != null) {
            String pd = PasswordUtil.md5Password(record.getUserName(), record.getPassword().toUpperCase());
            record.setPassword(pd);
        }
        record.setId(UUID.randomUUID().toString().replace("-", ""));
        record.setCreateTime(new Date());
        userMapper.insert(record);
    }

    public void adminInsert(UserDto record) {
        if (record.getPassword() != null) {
            String pd = PasswordUtil.md5Password(record.getUserName(), record.getPassword().toUpperCase());
            record.setPassword(pd);
        }
        record.setCreateTime(DateUtil.getStrTime());
        userMapper.adminInsert(record);
        if (!record.getId().isEmpty()) {
            if (record.getRoleId() != null) {
                UserRole userRole = new UserRole();
                userRole.setRoleId(record.getRoleId());
                userRole.setUserId(record.getId());
                userRoleMapper.insert(userRole);
            }
        }
    }

    public void update(User record) {
        if (record.getPassword() != null) {
            String pd = PasswordUtil.md5Password(record.getUserName(), record.getPassword().toUpperCase());
            record.setPassword(pd);
        }
        userMapper.updateById(record);
    }

    public void adminUpdate(UserDto record) {
        if (record.getPassword() != null) {
            String pd = PasswordUtil.md5Password(record.getUserName(), record.getPassword().toUpperCase());
            record.setPassword(pd);
        }
        userMapper.adminUpdate(record);
        if (record.getRoleId() != null) {
            userRoleMapper.delete(new QueryWrapper<UserRole>().lambda().eq(UserRole::getUserId, record.getId()));
            UserRole userRole = new UserRole();
            userRole.setRoleId(record.getRoleId());
            userRole.setUserId(record.getId());
            userRoleMapper.insert(userRole);
        }
    }


    public void del(String id) {
        userMapper.deleteById(id);
    }


    public User selectByPrimaryKey(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public UserDto selectById(String id) {
        return userMapper.selectById(id);
    }


    public List<User> getList(User record) {
        return userMapper.selectByRecord(record);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }


    public PageInfo<UserDto> pageList(UserDto record, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<UserDto> list = userMapper.selectAdminByRecord(record);
        return new PageInfo<>(list);
    }

    public User getByName(String name) {
        return userMapper.getByName(name);
    }


    public User getUser(String token) {
        Object obj = redisUtil.hget(RedisConstant.LOGINUSER, token);
        if (obj == null) {
            return null;
        }
        return (User) obj;
    }



    public RoleDto getByRole(String userId) {
        RoleDto roleDto = roleMapper.getByRole(userId);
        if (roleDto != null) {
            List<Permission> permissions = permissionMapper.getRoleAllById(roleDto.getId(), 1);
            roleDto.setPermissions(permissions);
        }
        return roleDto;
    }

    public boolean checkUserNameExist(User user) {
        User userRes = userMapper.checkUserNameExist(user);
        if (userRes != null) {
            return true;
        } else {
            return false;
        }
    }


}